package com.bsoft.gol.hbs.service.impl;

import ctd.account.UserRoleToken;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.base.Dept;
import hcn.base.Organization;
import hcn.protocol.ods.hospitalAppointment.HospitalDeptInfoResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.bsoft.gol.hbs.service.SynchronizationDeptService;
import org.springframework.stereotype.Service;
import service.rpc.DeptInfoServiceIntf;
import service.rpc.OrganizationInfoIntf;
import com.bsoft.gol.hbs.utils.CommonUtils;

import java.util.*;

/**
 * @program: Myproject
 * @description: 同步科室
 * @author: zangsf
 * @create: 2022-06-01 14:33
 **/
@Service
@SsdevService("synchronizationDeptService")
public class SynchronizationDeptServiceImpl implements SynchronizationDeptService {
    private static final Logger logger = LoggerFactory.getLogger(SynchronizationDeptServiceImpl.class);
    @Autowired
    private OrganizationInfoIntf organizationInfoIntf;
    @Autowired
    private DeptInfoServiceIntf deptInfoServiceIntf;

    @Override
    @RpcService
    public void SynchronizationDeptByService() throws ControllerException {

        //这里的参数是摆设,-->查询到所有的机构-->deptType = "01"="临床"\r,"02"="医技","03" ="行政","04"="其他"--这里的分页写死
        List<Organization> organizationList = organizationInfoIntf.findAllOrgMain(1, 1000);
        if (CommonUtils.isNotEmpty(organizationList)) {
            for (Organization organization : organizationList) {
                List<HospitalDeptInfoResponse> deptList = deptInfoServiceIntf.queryDeptInfoFromHis(organization.getLocalOrgId());
                ArrayList<Dept> depts = new ArrayList<>();
                if (CommonUtils.isNotEmpty(deptList)){
                    for (HospitalDeptInfoResponse response : deptList) {
                        Dept dept = new Dept();
                        dept.setTentId(UserRoleToken.getCurrentTenantId());
                        dept.setOrgId(organization.getOrgId());
                        dept.setLocalOrgId(organization.getLocalOrgId());
                        dept.setLocalDeptId(response.getDeptCode());
                        dept.setName(response.getDeptName());
                        if (!response.getDeptCode().equals(response.getParentDept())){
                            dept.setLocalDeptPid(response.getParentDept());
                        }
                        depts.add(dept);
                    }
                }
                if (CommonUtils.isNotEmpty(depts)){
                    //将这些科室同步到bbp门户,现在门户信息都是以bbp为准,且保存bbp的同时在saveOneDept方法中也会在我们本地保存一份
                    List<Map<String, Object>> bbpDeptList = deptInfoServiceIntf.findByOrgId(organization.getOrgId());
                    depts.forEach(d -> deptInfoServiceIntf.saveOneDept(bbpDeptList, d));
                }
            }
        }
        logger.info("SynchronizationDeptByService()科室同步成功");
    }
}
